package cn.tedu.store.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.tedu.store.config.tengxun.MapUtil;
import cn.tedu.store.config.util.RedisService;
import cn.tedu.store.entity.Meeting;
import cn.tedu.store.entity.MeetingCheckRecode;
import cn.tedu.store.entity.SMeetingcheckrecode;
import cn.tedu.store.mapper.EmployeeMapper;
import cn.tedu.store.mapper.MeetingCheckRecodeMapper;
import cn.tedu.store.mapper.MeetingMapper;
import cn.tedu.store.service.MeetingCheckRecodeDtoService;
import cn.tedu.store.temperatureDetection.entity.dto.EmployeeDto;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 75936
 * @version 1.0
 * @project demo_hesuan
 * @description
 * @date 2023-01-05 15:51:37
 */
@Service
@Slf4j
public class MeetingCheckRecodeDtoServiceImpl implements MeetingCheckRecodeDtoService {
    @Autowired
    MeetingMapper meetingMapper;
    @Autowired
    EmployeeMapper empolyeeMapper;
    @Autowired
    MeetingCheckRecodeMapper meetingCheckRecodeMapper;
    @Autowired
    RedisService redisService;
    // 获取两个时间相差分钟数
    public static long getTime(String oldTime,String newTime) throws ParseException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long NTime =df.parse(newTime).getTime();
        //从对象中拿到时间
        long OTime = df.parse(oldTime).getTime();
        long diff=(NTime-OTime)/1000/60;
        return diff;
    }

   @Autowired
   private StringRedisTemplate stringRedisTemplate;
    @Override
    public void getMeetingCheckRecodeDto(String mCode,
                                         Double lat ,
                                         Double lng,
                                         String iphone,
                                         String code) {
        //二维码作废间隔
        Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
        String keyObjectValue = redisService.getKeyObjectValue(mCode+code, String.class);

        if (!Objects.equals(code, keyObjectValue)){
            throw new RuntimeException("二维码已经过期，请重新扫面二维码");
        }
        Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
        Object lng2 = urlContent2.get("lng");
        Object lat2 = urlContent2.get("lat");
        double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
        if (distance>500){
            throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
        }
        EmployeeDto employeesByPhone = empolyeeMapper.findEmployeesByPhone(iphone);
        if (Objects.isNull(employeesByPhone)){
            throw new RuntimeException("非本院员工签到失败");
        }
        /**
         * 判断完位置，符合签到条件后将，员工的签到信息存入签到表
         * 需要的信息有
         * 参会人员编号 参会人的姓名 参会人的电话 参会签到的时间 参会的科室编号 参会人科室名称 有效性 会议名称 是否申请发言
         * 是否请假 请假批准人工号 是否替换 会议开始的时间
         *  (M_CODE,P_CODE,P_NAME,P_PHONE,P_BEGIN_DATE,P_DEPT_NAME
         *         ,P_VALIDITY,M_NAME,P_REPORT,P_REPORT_DATE,P_LEAVE,P_LEAVE_DATE
         *         ,P_LEAVE_RATIFY,P_REPLACE,M_BEGINDATE,P_ADDRESS
         *         )
         * */
        MeetingCheckRecode meetingCheckRecode1 = meetingMapper.selectSignEmployee(mCode, iphone);
        MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
        meetingCheckRecode.setMeetingCode(mCode);
        meetingCheckRecode.setEmpCode(employeesByPhone.getEmplCode());
        meetingCheckRecode.setEmpName(employeesByPhone.getEmplName());
        //REMARK数据库里面的字段代表手机号码
        meetingCheckRecode.setEmpPhone(employeesByPhone.getRemark());
        meetingCheckRecode.setEmpBeginDate(new Date());
        meetingCheckRecode.setEmpDeptCode(employeesByPhone.getDeptCode());
        Date date = new Date();
        Date parse = DateUtil.parse(date.toString());
        meetingCheckRecode.setEmpBeginDate(parse);
        meetingCheckRecode.setEmpDeptName(employeesByPhone.getDeptName());
        meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
        meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
        meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());
        meetingCheckRecode.setEmpBeginDate(date);
        meetingCheckRecode.setPFirm("枝江市人民医院");
        if(ObjectUtil.isEmpty(meetingCheckRecode1)){
            //修改
            if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                meetingCheckRecode.setEmpMeetingvalid("1");
            } else {
                meetingCheckRecode.setEmpMeetingvalid("2");
            }
            meetingCheckRecodeMapper.addMeetingCheckRecode1(meetingCheckRecode);
        }else  if (meetingCheckRecode1.getEmpMeetingvalid().equals("1") ||meetingCheckRecode1.getEmpMeetingvalid().equals("2")) {
            if (StringUtils.isBlank(hospitalMeeting.getMEnd())) {
                throw new RuntimeException("未设置二次签到暂时无法签到");
            } else if (
                StringUtils.isBlank(meetingCheckRecode1.getEmpLeaveRatify())){
                meetingCheckRecode1.setEmpMeetingvalid("二次签到");
                meetingCheckRecode1.setEmpBeginDate(new Date());
                meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode1);
            } else {throw  new RuntimeException("已进行二次签到请勿继续签到"); }
            } else if (meetingCheckRecode1.getEmpMeetingvalid().equals("0") ||meetingCheckRecode1.getEmpMeetingvalid().equals("4") ){
                //修改
                if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                    meetingCheckRecode.setEmpMeetingvalid("1");
                } else {
                    meetingCheckRecode.setEmpMeetingvalid("2");
                }
            meetingCheckRecodeMapper.addMeetingCheckRecode(meetingCheckRecode);
            }else if (meetingCheckRecode1.getEmpMeetingvalid().equals("3") || meetingCheckRecode1.getEmpMeetingvalid().equals("5"))
            {
            throw new RuntimeException("已请假无法签到");
            }else {
            throw new RuntimeException("请勿重复签到");
            }
        }



    //替签
    @Override
    public void replace(String mCode, Double lat, Double lng, String iphone, String code, String name) {
        Date date = new Date();
        Date parse = DateUtil.parse(date.toString());
        if (name.length()>=6){

            SimpleDateFormat sfp = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
            Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
            if (ObjectUtil.isEmpty(hospitalMeeting)){
              throw new RuntimeException("会议不存在");
            }
            Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
            Object lng2 = urlContent2.get("lng");
            Object lat2 = urlContent2.get("lat");
            double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
            if (distance>500){
                throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
            }
            //查  代签人 签到信息
            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName1(mCode,name);
            if (Objects.isNull(sMeetingcheckrecode)||sMeetingcheckrecode.getPValidity().equals("4")){
                throw new RuntimeException("不是本会议职工,请重新输入");
            }
            EmployeeDto employee = empolyeeMapper.findOne(name);
           SMeetingcheckrecode s2 = meetingCheckRecodeMapper.findPhone(mCode,iphone);

            if(sMeetingcheckrecode.getPCode().equals(s2.getPCode())){
                throw new RuntimeException("本人无法给自己代签");
            }
            EmployeeDto employeesByPhone = empolyeeMapper.findEmployeesByPhone(iphone);
            if (Objects.isNull(employee)){
                throw new RuntimeException("非本院员工签到失败");
            }
            if(sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2")){

                if (StringUtils.isBlank(hospitalMeeting.getMEnd())){
                    throw new RuntimeException("未设置二次签到不能继续签到");
                }else if (StringUtils.isBlank(sMeetingcheckrecode.getPLeaveRatify())){
                meetingCheckRecode.setEmpBeginDate(new Date());
                meetingCheckRecode.setEmpCode(sMeetingcheckrecode.getPCode());
                meetingCheckRecode.setMeetingCode(sMeetingcheckrecode.getMCode());
                meetingCheckRecode.setEmpMeetingvalid("二次签到");
                meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode);}else {
                    throw  new RuntimeException("已进行二次签到请勿继续签到");
                }

            }else if (sMeetingcheckrecode.getPValidity().equals("3") ) {

                    throw new RuntimeException("已请假无法签到");
                }else if ( sMeetingcheckrecode.getPValidity().equals("4")||sMeetingcheckrecode.getPValidity().equals("0")){
                meetingCheckRecode.setMeetingCode(mCode);
                meetingCheckRecode.setEmpCode(sMeetingcheckrecode.getPCode());
                meetingCheckRecode.setEmpName(sMeetingcheckrecode.getPName());
                //REMARK数据库里面的字段代表手机号码
                meetingCheckRecode.setEmpPhone(sMeetingcheckrecode.getPPhone());
                meetingCheckRecode.setEmpBeginDate(new Date());
                //meetingCheckRecode.setPBeginDate(new Date());
                meetingCheckRecode.setEmpDeptCode(sMeetingcheckrecode.getPDeptCode());

                meetingCheckRecode.setEmpBeginDate(parse);
                meetingCheckRecode.setEmpDeptName(sMeetingcheckrecode.getPDeptName());
                meetingCheckRecode.setEmpBeginDate(parse);
                meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
                meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
                meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());

                String format = sfp.format(date);


                try {
                    Date date1 = sfp.parse(format);
                    meetingCheckRecode.setEmpBeginDate(date1);
                    System.out.println(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                meetingCheckRecode.setEmpRemark2(employeesByPhone.getEmplCode() + employeesByPhone.getEmplName());
                // meetingCheckRecode.setEmpRemark3("职工");
                //修改
                if (hospitalMeeting.getMeetingBegindate().getTime() >= new Date().getTime()) {
                    meetingCheckRecode.setEmpMeetingvalid("1");
                    meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
                } else {
                    meetingCheckRecode.setEmpMeetingvalid("2");
                    meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
                }
                }else { throw new RuntimeException("请勿重复签到");

                }

        }else {


        SimpleDateFormat sfp = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
        Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
        if (ObjectUtil.isEmpty(hospitalMeeting)){
            return ;
        }
        Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
        Object lng2 = urlContent2.get("lng");
        Object lat2 = urlContent2.get("lat");
        double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
        if (distance>500){
            throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
        }

            //查是否本会议
            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName(mCode,name);
                    if (Objects.isNull(sMeetingcheckrecode)||sMeetingcheckrecode.getPValidity().equals("4")){
            throw new RuntimeException("不是本会议职工,请重新输入");
        }

        if(sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2") ){
            if (StringUtils.isBlank(hospitalMeeting.getMEnd())){
                throw new RuntimeException("未设置二次签到不能继续签到");
            }else if (StringUtils.isBlank(sMeetingcheckrecode.getPLeaveRatify())){
                meetingCheckRecode.setEmpBeginDate(new Date());
                meetingCheckRecode.setEmpCode(sMeetingcheckrecode.getPCode());
                meetingCheckRecode.setMeetingCode(sMeetingcheckrecode.getMCode());
                meetingCheckRecode.setEmpMeetingvalid("二次签到");
                meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode);}else {
                throw  new RuntimeException("已进行二次签到请勿继续签到");
            }
        }else if (sMeetingcheckrecode.getPValidity().equals("0")||sMeetingcheckrecode.getPValidity().equals("4") ){
            meetingCheckRecode.setMeetingCode(mCode);
            meetingCheckRecode.setEmpCode(sMeetingcheckrecode.getPCode());
            meetingCheckRecode.setEmpName(name);
            //REMARK数据库里面的字段代表手机号码
            meetingCheckRecode.setEmpPhone(sMeetingcheckrecode.getPPhone());
            meetingCheckRecode.setEmpBeginDate(new Date());
            //meetingCheckRecode.setPBeginDate(new Date());
            meetingCheckRecode.setEmpDeptCode(sMeetingcheckrecode.getPDeptCode());
            meetingCheckRecode.setEmpBeginDate(parse);
            meetingCheckRecode.setEmpDeptName(sMeetingcheckrecode.getPDeptName());
            meetingCheckRecode.setEmpBeginDate(parse);
            meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
            meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
            meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());

            String format = sfp.format(date);
            EmployeeDto employeesByPhone = empolyeeMapper.findEmployeesByPhone(iphone);

            try {
                Date date1 = sfp.parse(format);
                meetingCheckRecode.setEmpBeginDate(date1);
                System.out.println(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            meetingCheckRecode.setEmpRemark2(employeesByPhone.getEmplCode() + employeesByPhone.getEmplName());
            // meetingCheckRecode.setEmpRemark3("职工");
            //修改
            if (hospitalMeeting.getMeetingBegindate().getTime() >= new Date().getTime()) {
                meetingCheckRecode.setEmpMeetingvalid("1");
                meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
            } else {
                meetingCheckRecode.setEmpMeetingvalid("2");
                meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
            }
            }else {
                throw new RuntimeException("请勿重复签到");


        }
        }
    }

    /**
     * 院外员工签到
     * @param mCode
     * @param lat
     * @param lng
     * @param code
     */
    @Override
    public void getOutSideMeetingCheckRecodeDto(String mCode, Double lat, Double lng, String iphone, String code, String name,String add) {
        Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
        if (ObjectUtil.isEmpty(hospitalMeeting)){
            throw new RuntimeException("会议号有误请重新扫瞄二维码");
        }
        Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
        Object lng2 = urlContent2.get("lng");
        Object lat2 = urlContent2.get("lat");
        double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
        if (distance>500){
            throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
        }
        MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
        meetingCheckRecode.setMeetingCode(mCode);
        meetingCheckRecode.setEmpCode(iphone.substring(5));
        meetingCheckRecode.setEmpName(name);
        //REMARK数据库里面的字段代表手机号码
        meetingCheckRecode.setEmpPhone(iphone);
        meetingCheckRecode.setEmpBeginDate(new Date());
        meetingCheckRecode.setEmpDeptCode("无");
        Date date = new Date();
        Date parse = DateUtil.parse(date.toString());
        meetingCheckRecode.setEmpBeginDate(parse);
        meetingCheckRecode.setEmpDeptName("无");
        meetingCheckRecode.setEmpRemark3("非职工");
        meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
        meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
        meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());
        meetingCheckRecode.setEmpBeginDate(date);
        meetingCheckRecode.setPFirm(add);
        long l = meetingMapper.selectSignEmployeeSize(mCode, iphone);
        if(l>0) {
            MeetingCheckRecode meetingCheckRecode1 = meetingMapper.selectSignEmployee(mCode, iphone);
            meetingCheckRecode.setEmpCode(meetingCheckRecode1.getEmpCode());
        if (meetingCheckRecode1.getEmpMeetingvalid().equals("1")||meetingCheckRecode1.getEmpMeetingvalid().equals("2")){
            if (StringUtils.isBlank(hospitalMeeting.getMEnd())){
                throw new RuntimeException("未设置二次签到不能继续签到");
            }else if(StringUtils.isBlank(meetingCheckRecode1.getEmpLeaveRatify())) {
                meetingCheckRecode.setEmpMeetingvalid("二次签到");
                meetingCheckRecode.setEmpBeginDate(new Date());
                meetingCheckRecode.setEmpCode(meetingCheckRecode1.getEmpCode());
                meetingCheckRecode.setMeetingCode(meetingCheckRecode1.getMeetingCode());
                meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode);
            }else {
                throw new RuntimeException("已进行二次签到请勿继续签到");
            }
        }else if (meetingCheckRecode1.getEmpMeetingvalid().equals("0")||meetingCheckRecode1.getEmpMeetingvalid().equals("4")){
            if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                meetingCheckRecode.setEmpMeetingvalid("1");
            } else {
                meetingCheckRecode.setEmpMeetingvalid("2");
            }
            meetingCheckRecodeMapper.addMeetingCheckRecode(meetingCheckRecode);
        }else {
            throw new RuntimeException("请勿重复签到");
        }
        }else {
            if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                meetingCheckRecode.setEmpMeetingvalid("1");
            } else {
                meetingCheckRecode.setEmpMeetingvalid("2");
            }
            meetingCheckRecodeMapper.addMeetingOutSide(meetingCheckRecode);
        }
    }

    @Override
    public List<String> getList() {
        List<EmployeeDto> employeesByPhone = empolyeeMapper.findList();
        ArrayList<String> strings = new ArrayList<>();
        for (EmployeeDto employeeDto : employeesByPhone) {
            strings.add(employeeDto.getEmplCode()+employeeDto.getEmplName());
        }
        return strings;
    }

    @Override
    public List<SMeetingcheckrecode> getAll(String iphone) {

        List<SMeetingcheckrecode>  all = meetingMapper.getAll(iphone);

        return all;
    }

    //请假
    @Override
    public void update(String mCode,String iphone) {
        MeetingCheckRecode meetingCheckRecode = meetingMapper.selectSignEmployee(mCode, iphone);
       if(meetingCheckRecode.getEmpMeetingvalid().equals("5")){
           throw new RuntimeException("你已请假无法在请假");
       }
       if(meetingCheckRecode.getEmpMeetingvalid().equals("1")){
           throw new RuntimeException("你已签到无法请假");
       }
        if(!meetingCheckRecode.getEmpMeetingvalid().equals("0")){
            throw new RuntimeException("此状态无发请假");
        }
        String code = "5";//请假   5.审批中
        Date date = new Date();
        meetingMapper.leave(mCode,iphone,code,date);
    }

    @Override
    public void replace2(String mCode, Double lat, Double lng, String iphone, String code, String name, String name2) {
        if (name2.length()>=6){
            SimpleDateFormat sfp = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
            Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
            if (ObjectUtil.isEmpty(hospitalMeeting)){
                return ;
            }
            Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
            Object lng2 = urlContent2.get("lng");
            Object lat2 = urlContent2.get("lat");
            double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
            if (distance>500){
                throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
            }
            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName1(mCode,name2);

                if (Objects.isNull(sMeetingcheckrecode)){
                    throw new RuntimeException("本会议不存在该员工代签失败");
                }
            EmployeeDto employee = empolyeeMapper.findOne(name2);


            if (sMeetingcheckrecode.getPValidity().equals("二次签到")){
                throw new RuntimeException("请勿重复签到");
            }
            if(sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2")){
//                if (sMeetingcheckrecode.getPBeginDate().getTime()+600000<=new Date().getTime()) {
                if (StringUtils.isBlank(hospitalMeeting.getMEnd())){
                    throw new RuntimeException("未设置二次签到不能继续签到");
                }else if (StringUtils.isBlank(sMeetingcheckrecode.getPLeaveRatify())){

                    MeetingCheckRecode meetingCheckRecode1 = new MeetingCheckRecode();
                    meetingCheckRecode1.setEmpMeetingvalid("二次签到");
                    meetingCheckRecode1.setMeetingCode(sMeetingcheckrecode.getMCode());
                    meetingCheckRecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                    meetingCheckRecode1.setEmpBeginDate(new Date());
                    meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode1);
                }else {
                    throw  new RuntimeException("已进行二次签到请勿继续签到");
                }

            }else if (sMeetingcheckrecode.getPValidity().equals("3") ) {
                throw new RuntimeException("已请假无法签到");
            }else if ( sMeetingcheckrecode.getPValidity().equals("5")){
                throw new RuntimeException("已请假无法签到");
            } else {

                meetingCheckRecode.setMeetingCode(mCode);
                meetingCheckRecode.setEmpCode(sMeetingcheckrecode.getPCode());
                meetingCheckRecode.setEmpName(sMeetingcheckrecode.getPName());
                //REMARK数据库里面的字段代表手机号码
                meetingCheckRecode.setEmpPhone(sMeetingcheckrecode.getPPhone());
                meetingCheckRecode.setEmpBeginDate(new Date());
                //meetingCheckRecode.setPBeginDate(new Date());
                meetingCheckRecode.setEmpDeptCode(sMeetingcheckrecode.getPDeptCode());
                Date date = new Date();
                Date parse = DateUtil.parse(date.toString());
                meetingCheckRecode.setEmpBeginDate(parse);
                meetingCheckRecode.setEmpDeptName(sMeetingcheckrecode.getPDeptName());
                meetingCheckRecode.setEmpBeginDate(parse);
                meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
                meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
                meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());

                String format = sfp.format(date);


                try {
                    Date date1 = sfp.parse(format);
                    meetingCheckRecode.setEmpBeginDate(date1);
                    System.out.println(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                meetingCheckRecode.setEmpRemark2(name);
                // meetingCheckRecode.setEmpRemark3("职工");
                //修改
                if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                    meetingCheckRecode.setEmpMeetingvalid("1");
                    meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
                } else {
                    meetingCheckRecode.setEmpMeetingvalid("2");
                    meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
                }
            }

        }else {

//            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName1(mCode,name2);
            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName(mCode,name2);
            if (Objects.isNull(sMeetingcheckrecode)){
                throw new RuntimeException("本会议不存在该员工代签失败");
            }
            SimpleDateFormat sfp = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
            Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
            if (ObjectUtil.isEmpty(hospitalMeeting)){
                throw new RuntimeException("该会议不存在");
            }
            Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
            Object lng2 = urlContent2.get("lng");
            Object lat2 = urlContent2.get("lat");
            double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
            if (distance>500){
                throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
            }

            if (sMeetingcheckrecode.getPValidity().equals("二次签到")){
                throw new RuntimeException("请勿重复签到");
            }

            if(sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2")){
//                if (sMeetingcheckrecode.getPBeginDate().getTime()+600000<=new Date().getTime()){
                if (StringUtils.isBlank(hospitalMeeting.getMEnd())){
                    throw new RuntimeException("未设置二次签到不能继续签到");
                } else {
                    MeetingCheckRecode meetingCheckRecode1 = new MeetingCheckRecode();
                    meetingCheckRecode1.setEmpMeetingvalid("二次签到");
                    meetingCheckRecode1.setMeetingCode(sMeetingcheckrecode.getMCode());
                    meetingCheckRecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                    meetingCheckRecode1.setEmpBeginDate(new Date());
                    meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode1);
                }

//                }else {
//                    throw new RuntimeException("已签到请勿重复签到");
//                }
            }else if(sMeetingcheckrecode.getPValidity().equals("3")){
                throw new RuntimeException("已请假无法签到");
            }else if (sMeetingcheckrecode.getPValidity().equals("5")){
                throw new RuntimeException("已请假无法签到");
            }else {

            meetingCheckRecode.setMeetingCode(mCode);
            meetingCheckRecode.setEmpCode(sMeetingcheckrecode.getPCode());
            meetingCheckRecode.setEmpName(name2);
            //REMARK数据库里面的字段代表手机号码
            meetingCheckRecode.setEmpPhone(sMeetingcheckrecode.getPPhone());
            meetingCheckRecode.setEmpBeginDate(new Date());
            //meetingCheckRecode.setPBeginDate(new Date());
            meetingCheckRecode.setEmpDeptCode(sMeetingcheckrecode.getPDeptCode());
            Date date = new Date();
            Date parse = DateUtil.parse(date.toString());
            meetingCheckRecode.setEmpBeginDate(parse);
            meetingCheckRecode.setEmpDeptName(sMeetingcheckrecode.getPDeptName());
            meetingCheckRecode.setEmpBeginDate(parse);
            meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
            meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
            meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());

            String format = sfp.format(date);
            EmployeeDto employeesByPhone = empolyeeMapper.findEmployeesByPhone(iphone);

            try {
                Date date1 = sfp.parse(format);
                meetingCheckRecode.setEmpBeginDate(date1);
                System.out.println(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            meetingCheckRecode.setEmpRemark2(name);
            // meetingCheckRecode.setEmpRemark3("职工");
            //修改
            if(hospitalMeeting.getMeetingBegindate().getTime()>new Date().getTime()){
                meetingCheckRecode.setEmpMeetingvalid("1");
                meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
            }else {
                meetingCheckRecode.setEmpMeetingvalid("2");
                meetingCheckRecodeMapper.replaceMeetingCheckRecode(meetingCheckRecode);
            }

        }
        }
    }

    //输入员工号进行签到
    @Override
    public void getEmplCodeCheck(String mCode, Double lng, Double lat, String code, String pCode) {
        Date parse = DateUtil.parse(new Date().toString());
        //查询会议
        Meeting hospitalMeeting = meetingMapper.findBymeetingCode(mCode);
        //地理位置比对
        Map<String, Object> urlContent2 = MapUtil.getURLContent(hospitalMeeting.getMeetingAddress());
        Object lng2 = urlContent2.get("lng");
        Object lat2 = urlContent2.get("lat");
        double distance = MapUtil.getDistance(Double.parseDouble(lat2.toString()), Double.parseDouble(lng2.toString()), lat,lng);
        if (distance>500){
            throw new RuntimeException("签到失败,未在指定范围内签到,请到指定的范围内签到");
        }
        EmployeeDto employees = empolyeeMapper.findEmployeesByPCode(pCode);
        if (Objects.isNull(employees)){
            throw new RuntimeException("该员工不存在");
        }
        SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName(mCode,employees.getEmplName());
        MeetingCheckRecode meetingCheckRecode = new MeetingCheckRecode();
        meetingCheckRecode.setMeetingCode(mCode);
        meetingCheckRecode.setEmpCode(employees.getEmplCode());
        meetingCheckRecode.setEmpName(employees.getEmplName());
        //REMARK数据库里面的字段代表手机号码
        meetingCheckRecode.setEmpPhone(employees.getRemark());
        meetingCheckRecode.setEmpBeginDate(new Date());
        meetingCheckRecode.setEmpDeptCode("无");
        meetingCheckRecode.setEmpBeginDate(parse);
        meetingCheckRecode.setEmpDeptName("无");
        meetingCheckRecode.setEmpRemark3("职工");
        meetingCheckRecode.setMeetingName(hospitalMeeting.getMeetingName());
        meetingCheckRecode.setMeetingBeginDate(hospitalMeeting.getMeetingBegindate());
        meetingCheckRecode.setPAddress(hospitalMeeting.getMeetingAddress());
        meetingCheckRecode.setEmpBeginDate(new Date());
        if (Objects.isNull(sMeetingcheckrecode)){
            if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                meetingCheckRecode.setEmpMeetingvalid("1");
            } else {
                meetingCheckRecode.setEmpMeetingvalid("2");
            }
            meetingCheckRecodeMapper.addMeetingCheckRecode1(meetingCheckRecode);
            return;
        }else {
            //等于0经行签到不等于1或2进行二次签到
            if(sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2")){
                if (StringUtils.isBlank(hospitalMeeting.getMEnd())){
                    throw new RuntimeException("未设置二次签到请勿继续签到");
                }else if (StringUtils.isBlank(sMeetingcheckrecode.getPLeaveRatify())){
                    meetingCheckRecode.setEmpMeetingvalid("二次签到");
                    meetingCheckRecode.setEmpEndDate(parse);
                    meetingCheckRecode.setEmpBeginDate(parse);
                    meetingCheckRecodeMapper.addMeetingCheckRecode2(meetingCheckRecode);
                }else {
                    throw new RuntimeException("已进行二次签到请勿继续签到");
                }
            }else if (sMeetingcheckrecode.getPValidity().equals("0")||sMeetingcheckrecode.getPValidity().equals("4")){
                meetingCheckRecode.setEmpBeginDate(parse);
                if (hospitalMeeting.getMeetingBegindate().getTime() > new Date().getTime()) {
                    meetingCheckRecode.setEmpMeetingvalid("1");
                } else {
                    meetingCheckRecode.setEmpMeetingvalid("2");
                }
                meetingCheckRecodeMapper.addMeetingCheckRecode(meetingCheckRecode);
            }else if (sMeetingcheckrecode.getPValidity().equals("3")||sMeetingcheckrecode.getPValidity().equals("5")){
                throw new RuntimeException("已请假无法签到");
            }else {
                throw new RuntimeException("已签到请勿重复签到");
            }
        }




    }
    //代替签到
    @Override
    public void getEmplCodeCheckReplace(String mCode, Double lng, Double lat, String code, String pCode, String nameCode) {
        Date date = new Date();
        Date parse = DateUtil.parse(date.toString());
        MeetingCheckRecode sMeetingcheckrecode1 = new MeetingCheckRecode();
        Meeting bymeetingCode = meetingMapper.findBymeetingCode(mCode);
        EmployeeDto byEmplCode = empolyeeMapper.getByEmplCode(pCode);
        if (nameCode.length()>=6){
            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName1(mCode, nameCode);
            if (Objects.isNull(sMeetingcheckrecode)){
                throw new RuntimeException("不是本会议员工请直接签到");
            }
            if (sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2")){
                    if (StringUtils.isBlank(bymeetingCode.getMEnd())){
                        throw  new RuntimeException("未设置二次签到请勿继续签到");
                    }else if (StringUtils.isBlank(sMeetingcheckrecode.getPLeaveRatify())){
                        sMeetingcheckrecode1.setEmpMeetingvalid("二次签到");
                        sMeetingcheckrecode1.setEmpBeginDate(parse);
                        sMeetingcheckrecode1.setEmpEndDate(parse);
                        sMeetingcheckrecode1.setMeetingCode(mCode);
                        sMeetingcheckrecode1.setEmpCode(sMeetingcheckrecode.getPCode());

                        meetingCheckRecodeMapper.addMeetingCheckRecode2(sMeetingcheckrecode1);
                    }else {
                        throw  new RuntimeException("已进行二次签到请勿继续签到");
                    }

            }else if (sMeetingcheckrecode.getPValidity().equals("0")||sMeetingcheckrecode.getPValidity().equals("4")){
                //
                if (bymeetingCode.getMeetingBegindate().getTime()>new Date().getTime()){
                    sMeetingcheckrecode1.setEmpMeetingvalid("1");
                }else {
                    sMeetingcheckrecode1.setEmpMeetingvalid("2");
                }
                sMeetingcheckrecode1.setEmpRemark2(pCode+byEmplCode.getEmplName());
                sMeetingcheckrecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                sMeetingcheckrecode1.setMeetingCode(sMeetingcheckrecode.getMCode());
                sMeetingcheckrecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                sMeetingcheckrecode1.setEmpBeginDate(parse);
                meetingCheckRecodeMapper.updateMeetingCheckRecode(sMeetingcheckrecode1);
            }else if (sMeetingcheckrecode.getPValidity().equals("3")||sMeetingcheckrecode.getPValidity().equals("5")){
               throw  new RuntimeException("已请假无法签到");
            }else {
                throw new RuntimeException("请勿重复签到");
            }


        }else {
            SMeetingcheckrecode sMeetingcheckrecode = meetingCheckRecodeMapper.findName(mCode, nameCode);

            if (Objects.isNull(sMeetingcheckrecode)){
                throw new RuntimeException("不是本会议员工请直接签到");
            }

            if (sMeetingcheckrecode.getPValidity().equals("1")||sMeetingcheckrecode.getPValidity().equals("2")){
                if (StringUtils.isBlank(bymeetingCode.getMEnd())){
                    throw  new RuntimeException("未设置二次签到请勿继续签到");
                }else if (StringUtils.isBlank(sMeetingcheckrecode.getPLeaveRatify())){
                    sMeetingcheckrecode1.setEmpMeetingvalid("二次签到");
                    sMeetingcheckrecode1.setEmpBeginDate(parse);
                    sMeetingcheckrecode1.setMeetingCode(mCode);
                    sMeetingcheckrecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                    meetingCheckRecodeMapper.addMeetingCheckRecode2(sMeetingcheckrecode1);
                }else {
                    throw  new RuntimeException("已进行二次签到请勿继续签到");
                }

            }else if (sMeetingcheckrecode.getPValidity().equals("0")||sMeetingcheckrecode.getPValidity().equals("4")){
                if (bymeetingCode.getMeetingBegindate().getTime()>new Date().getTime()){
                    sMeetingcheckrecode1.setEmpMeetingvalid("1");
                }else {
                    sMeetingcheckrecode1.setEmpMeetingvalid("2");
                }
                sMeetingcheckrecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                sMeetingcheckrecode1.setMeetingCode(sMeetingcheckrecode.getMCode());
                sMeetingcheckrecode1.setEmpCode(sMeetingcheckrecode.getPCode());
                sMeetingcheckrecode1.setEmpRemark2(pCode+byEmplCode.getEmplName());
                sMeetingcheckrecode1.setEmpBeginDate(parse);
                meetingCheckRecodeMapper.updateMeetingCheckRecode(sMeetingcheckrecode1);
            }else if (sMeetingcheckrecode.getPValidity().equals("3")||sMeetingcheckrecode.getPValidity().equals("5")){
                throw  new RuntimeException("已请假无法签到");
            }else {
                throw  new RuntimeException("请勿重复签到");
            }


        }


    }


}
